package com.region.plugin.proxy;

import com.region.plugin.Interceptor;
import com.region.plugin.ProceedPoint;

/**
 * @author liujieyu
 * @date 2024/11/6 10:28
 * @desciption
 */
public abstract class AbstractRealProxy {

    /**
     * target object like HttpExecutor/Interceptor
     */
    protected Object target;

    /**
     * interceptor object
     */
    protected Interceptor interceptor;

    public AbstractRealProxy(Object target, Interceptor interceptor) {
        this.target = target;
        this.interceptor = interceptor;
    }

    /**
     * The pointcut is performed through the abstract template method
     *
     * @param interceptor
     * @param proceedPoint
     * @return
     */
    protected Object executeProceed(Interceptor interceptor, ProceedPoint proceedPoint) throws Throwable {
        Object relVal = null;
        try {
            // Execution of predicate methods
            interceptor.doBefore();
            relVal = interceptor.proceed(proceedPoint);
        } catch (Throwable throwable) {
            // Execution of post-exception method
            interceptor.doAfterThrowing(throwable);
        }
        // Execution of the posterior method
        interceptor.doAfter();
        return relVal;
    }

}
